package com.test.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.test.model.UserInfo;
import com.test.service.UserService;

@Controller
@RequestMapping("/user")
public class IndexController extends BaseController
{
	@Resource
	UserService uService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView all()
	{
		List<UserInfo> users = uService.getAllUsers();
		ModelAndView view = new ModelAndView("index");
		view.addObject("users", users);
		System.out.println("------------all--------------");
		return view;
	}

	@RequestMapping("/all")
	public ModelAndView allUser()
	{
		ModelAndView view = new ModelAndView("index");
		List<Map<String, Object>> users = uService.getAllUsersMap();
		view.addObject("users", users);
		System.out.println("------------allUser--------------");
		return view;
	}

	@RequestMapping(value = "/one/{id}")
	public ModelAndView oneUser(@PathVariable Integer id)
	{
		ModelAndView view = new ModelAndView("index");
		UserInfo user = uService.getUser(id);
		List<UserInfo> users = new ArrayList<UserInfo>();
		users.add(user);
		view.addObject("users", users);
		System.out.println("------------oneUser："+id+"--------------");
		return view;
	}
	
	@RequestMapping(value = "/add", method=RequestMethod.POST)
	public String addUser(UserInfo user){
		uService.addUser(user);
		return "redirect:all";
	}
}
